package com.kalenchuk.controller;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.annotation.PostConstruct;
import javax.swing.JFrame;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

import com.kalenchuk.dao.WireDao;

@Controller("applicationController")
public class ApplicationController {

	@Autowired()
	@Qualifier("mainApplicationWindow")
	private JFrame mainWindow;

	private ClassPathXmlApplicationContext context;

	@Autowired
	private WireDao wireDao;

	public ApplicationController() {

	}

	@PostConstruct
	public void addMainWindowListener() {
		mainWindow.addWindowListener(new WindowListener() {

			public void windowOpened(WindowEvent e) {
			}

			public void windowIconified(WindowEvent e) {
			}

			public void windowDeiconified(WindowEvent e) {
			}

			public void windowDeactivated(WindowEvent e) {
			}

			public void windowClosing(WindowEvent e) {
				try {
					if (wireDao != null) {
						wireDao.shutdown();
					}
					if (context != null) {
						context.close();
					}
				} catch (Exception ex) {

				}
			}

			public void windowClosed(WindowEvent e) {
			}

			public void windowActivated(WindowEvent e) {
			}
		});
	}

	public JFrame getMainWindow() {
		return mainWindow;
	}

	public void setMainWindow(JFrame mainWindow) {
		this.mainWindow = mainWindow;
	}

	public void setContext(ClassPathXmlApplicationContext context) {
		this.context = context;
	}

}
